package net.caixiaomi.info.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.model.ShareItem;

public class ShareAdapter extends BaseQuickAdapter<ShareItem, BaseViewHolder> {

    public ShareAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShareItem item) {
        try {
            TextView view = (TextView) helper.itemView;
            view.setId(item.getId());
            view.setText(item.getTitle());
            view.setCompoundDrawablesWithIntrinsicBounds(null, icon(item.getIcon()), null, null);
            view.setCompoundDrawablePadding((int) CommonApp.getInstance().getResources().getDimension(R.dimen.padding_8));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Drawable icon(Drawable drawable) {
        if (drawable != null) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            @SuppressWarnings("SuspiciousNameCombination") Drawable resizeIcon = new BitmapDrawable(CommonApp.getInstance().getResources(), Bitmap.createScaledBitmap(bitmap, (int) CommonApp.getInstance().getResources().getDimension(net.caixiaomi.info.R.dimen.padding_44), (int) CommonApp.getInstance().getResources().getDimension(net.caixiaomi.info.R.dimen.padding_44), true));
            Drawable.ConstantState state = resizeIcon.getConstantState();
            resizeIcon = DrawableCompat.wrap(state == null ? resizeIcon : state.newDrawable().mutate());
            return resizeIcon;
        }
        return null;
    }
}
